﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;
using System.Windows.Threading;
using System.Text;

namespace VAdvantage.Dialogs
{
    public partial class YTDialog : ChildWindow
    {
        #region Declaration

        bool isOOB = false;
        string videoID = "";

        #endregion


        private YTDialog()
        {
            InitializeComponent();
            isOOB = Application.Current.IsRunningOutOfBrowser;
            SetSize();
            this.Loaded += new RoutedEventHandler(YTDialog_Loaded);
            this.Closed += new EventHandler(YTDialog_Closed);
        }
        
        public YTDialog(string vID):this()
        {
            videoID = vID;
            
        }

        #region Events

        void YTDialog_Closed(object sender, EventArgs e)
        {
            CleanUp();
        }

        //private FrameworkElement root;
        //private FrameworkElement contentroot;
        //private FrameworkElement chrome;
        void YTDialog_Loaded(object sender, RoutedEventArgs e)
        {
            if (VisualTreeHelper.GetChildrenCount(this) == 0)
            {
                Dispatcher.BeginInvoke(() => YTDialog_Loaded(this, e));
                return;
            }
            //root = (FrameworkElement)VisualTreeHelper.GetChild(this, 0);
            //contentroot = root.FindName("ContentRoot") as FrameworkElement;
            //chrome = root.FindName("Chrome") as FrameworkElement;

           
            SetUI();

           // timer.Start();
            
        }

        double leftMargin,topMargin;


        private void SetSize()
        {
            Size s = Utility.Envs.GetHostSize();

            leftMargin = Convert.ToInt32(s.Width / 6);
            topMargin = Convert.ToInt32(s.Height / 8);
            this.Margin = new Thickness(leftMargin, topMargin, 0, 0);
            this.Height = Math.Max(s.Height - (2 * topMargin), 100);
            this.Width = Math.Max(s.Width - (2 * leftMargin), 100);
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
           // Close();
            OKButton.IsEnabled = false;
            this.DialogResult = true;
        }

        #endregion

        #region Function

        private void SetUI()
        {
            if (!isOOB)
            {

                HtmlElement m = HtmlPage.Document.GetElementById("_sl_youTube");
                if (m != null)
                {

                    string l = "102", t = "102", h = "100", w = "150";
                    GeneralTransform gtt = this.TransformToVisual(((Grid)Application.Current.RootVisual).Children[0]);
                    Point offsett = gtt.Transform(new Point(0, 0));
                    double tt = offsett.Y;
                    double tw = offsett.X;

                    //GeneralTransform gt = chrome.TransformToVisual(Application.Current.RootVisual);
                    //Point offset = gt.Transform(new Point(tw, tt));

                    t = (topMargin+12).ToString();
                    l = (leftMargin+12).ToString();
                    h = htmlHost.ActualHeight.ToString();
                    w = htmlHost.ActualWidth.ToString();

                   
              
                    //m.SetStyleAttribute("position", "absolute");
                  
                    m.SetStyleAttribute("height", h+"px");
                    m.SetStyleAttribute("width", w+"px");
                    m.SetStyleAttribute("top", t + "px");
                    m.SetStyleAttribute("left", l+"px");
                  
                   
                    m.SetAttribute("src", "http://www.youtube.com/embed/" + videoID.Trim()+ "?autoplay=1");
                    m.SetStyleAttribute("visibility", "visible");
                }
            }
            else
            {

                htmlHost.NavigateToString(GetYouTubeScript(videoID.Trim()));
            }
        }

        protected string GetYouTubeScript(string prmVideoId)
        {
           // string  localStrBld = @"<object style='height: 100%; width:100%'> ";
            StringBuilder localStrBld  = new StringBuilder( @"<object style='height: 100%; width:100%'> ");
            localStrBld.Append("<param name='movie' value='http://www.youtube.com/v/" + prmVideoId + "'></param> ");
            localStrBld.Append("<param name='allowFullScreen' value='true'></param> ");
            localStrBld.Append("<param name='allowscriptaccess' value='always'></param> ");
            localStrBld.Append("<embed src='http://www.youtube.com/v/" + prmVideoId + "?autoplay=1' ");
            localStrBld.Append("type='application/x-shockwave-flash' allowscriptaccess='always' ");
            localStrBld.Append("allowfullscreen='true' width='100%' height='100%'> ");
            localStrBld.Append("</embed></object>");
            return localStrBld.ToString();
        }

        private void CleanUp()
        {
            if (!isOOB)
            {
                HtmlElement m = HtmlPage.Document.GetElementById("_sl_youTube");
                if (m != null)
                {

                    //string l = "200", t = "50", h = "100", w = "150";
                    //GeneralTransform gt =  htmlHost.TransformToVisual(Application.Current.RootVisual as UIElement);
                    //Point offset = gt.Transform(new Point(0, 0));
                    //t = offset.Y.ToString();
                    //l = offset.X.ToString();
                    //h = htmlHost.ActualHeight.ToString();
                    //w = htmlHost.ActualWidth.ToString();
                    m.SetStyleAttribute("visibility", "hidden");
                    m.SetAttribute("src", "");
                    //m.SetStyleAttribute("left", l);
                    //m.SetStyleAttribute("top", t);
                    m.SetStyleAttribute("height", "0px");
                    m.SetStyleAttribute("width", "0px");
                    //m.SetStyleAttribute("position", "absolute");
                    
                }

            }
        }

        #endregion

    }
}

